package com.phone.handle;

import com.phone.entity.ServiceRecord;
import com.phone.exception.NoBindingIdException;
import org.hibernate.Session;

import java.util.List;

/**
 * Created by tisen on 2017/8/28.
 */
public class ServiceHandle {
    private Session session;

    public ServiceHandle(Session session) {
        this.session = session;
    }

    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }

    public void save(String serial, String model, boolean status) throws Exception {
//        ServiceRecord serviceRecord = new ServiceRecord(serial, model, status);
//        System.out.println(serviceRecord);
//        session.saveOrUpdate(serviceRecord);
    }

    public ServiceRecord get(String model) throws Exception {
        ServiceRecord serviceRecord = session.get(ServiceRecord.class, model);
        if (serviceRecord == null)
            throw new NoBindingIdException();
        return serviceRecord;
    }

    public List<ServiceRecord> search(String model, boolean isVague) {
        if (isVague) {
            return session.createQuery("from  ServiceRecord where model like '%" + model + "%'").list();
        } else {
            return session.createQuery("from  ServiceRecord where model = '" + model + "'").list();
        }
    }

    public List<ServiceRecord> search() {
        return session.createQuery("from  ServiceRecord ").list();
    }
}
